﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Xml.Linq;

namespace RMService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {     
        public void update(MasterSet set)
        {
            int computerID = 0;
            MasterSet mset = new MasterSet();
            mset = set;
            if (mset.mInfo.HostName.Length < 1)
            {
                mset.mInfo.HostName = "CMIST\\9999";
            }           
                string name = set.mInfo.HostName;
                int l = name.Length;
                int s = l - 4;
                string id = name.Substring(s, 4);
                int cid = int.Parse(id);            
            // create and use our database context
            using (ReserveMeDBDataContext rms = new ReserveMeDBDataContext())
            {               
                // get the machines record from the DB
                var x = (from c in rms.ReserveMeComputers where c.Tag == cid select c).ToList();
                if (x.Count > 0)
                {
                    computerID = (from computer in x where computer.Tag == cid select computer.ComputerID).FirstOrDefault();
                }                
                if (x.Count < 1)
                {
                    ReserveMeComputer rc = new ReserveMeComputer();
                    rc.Location = "9,9";
                    rc.RoomID = 2;
                    rc.Tag = cid;
                    rms.ReserveMeComputers.InsertOnSubmit(rc);
                    rms.SubmitChanges();
                    x = (from c in rms.ReserveMeComputers where c.Tag == cid select c).ToList();
                    computerID = (from computer in x where computer.Tag == cid select computer.ComputerID).FirstOrDefault();
                }




                // get the installed applications we have already on record for this machine from the DB
                var apps = (from a in rms.ReserveMeApplicationLists where a.ComputerID == computerID select a).ToList();

                /* ensure we even have a list of applications installed on record before we attempt to compare
                 * 
                 * this foreach loop in combination with a linq to sql query which iterates through the machines installed apps
                 * will select one that matches the name of the applications we have in our DB of that machine, should this record
                 * no longer exist on what the Clients Machine is reporting, then we delete this record as it has possibly been 
                 * uninstalled
                 * 
                 */
                if (apps.Count > 0 && mset.appInfo.appList.Count > 0)
                {
                    // iterate through each part of the installed application list
                    foreach (ReserveMeApplicationList ls in apps)
                    {
                        // find us the installed application that matches what we have on record
                        try
                        {
                            var keep = (from ap in mset.appInfo.appList where ap.Name.Trim() == ls.ReserveMeApplication.ApplicationName.Trim() select ap).First();
                        }
                        catch (Exception)
                        {
                            /*if (keep == null)
                            {*/
                                //tell the context to delete on submit this instance in our DB
                                rms.ReserveMeApplicationLists.DeleteOnSubmit(ls);
                           // }          
                        }
                        // if we didnt get any matches we delete
                                      
                    }
                    rms.SubmitChanges();
                }
                // ensure we even have a list of applications
                if (mset.appInfo.appList.Count > 0)
                {
                    //iterate through the list of applications our clients machine reports to have installed
                    foreach (App info in mset.appInfo.appList)
                    {
                        //select the records we have in our DB that match this 
                        try
                        {
                            var capps = (from a in rms.ReserveMeApplicationLists where a.ComputerID == computerID select a).ToList().Where(c => c.ReserveMeApplication.ApplicationName.Trim() == info.Name.Trim()).FirstOrDefault();
                            if (capps == null)
                            {
                                try
                                {
                                    ReserveMeApplicationList app = new ReserveMeApplicationList();
                                    var list = (from a in rms.ReserveMeApplications select a).ToList();
                                    var contains = (from one in list where one.ApplicationName.Trim().ToLower() == info.Name.Trim().ToLower() select one).FirstOrDefault();
                                    if (contains != null)
                                    {
                                        //var appid = (from a in list select a).ToList();
                                        app.ComputerID = computerID;
                                        app.ApplicationID = (from a in list where a.ApplicationName.Trim().ToLower() == info.Name.Trim().ToLower() select a.ApplicationID).First();
                                        rms.ReserveMeApplicationLists.InsertOnSubmit(app);
                                        rms.SubmitChanges();
                                    }
                                }
                                catch (Exception)
                                {
                                    break;
                                }
                            }
                            else
                            {
                            }
                        }
                        catch (Exception)
                        {
                          return;
                        }
                    }
                }
                if(mset.user.ToString().Length > 0)
                {
                    var us = (from u in rms.ReserveMeUsers where u.ClientID == mset.user select u).First();
                    var ax = (from computer in x where computer.Tag == cid select computer).First();
                    ax.UserID = mset.user;
                }
                else
                {
                    var ax = (from computer in x where computer.Tag == cid select computer).First();
                    ax.UserID = null;
                }
                rms.SubmitChanges();        
            }
        }               
    }
}
